Grokking Amazon Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Amazon Easy Problems
Redistribute Characters to Make All Strings Equal (easy)
Solution: Redistribute Characters to Make All Strings Equal
Longest Common Prefix (easy)
Solution: Longest Common Prefix
Make Array Zero by Subtracting Equal Amounts (easy)
Solution: Make Array Zero by Subtracting Equal Amounts
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Path Crossing (easy)
Solution: Path Crossing
Special Array With X Elements Greater Than or Equal X (easy)
Solution: Special Array With X Elements Greater Than or Equal X
Relative Sort Array (easy)
Solution: Relative Sort Array
Transpose Matrix (easy)
Solution: Transpose Matrix
Find Common Characters (easy)
Solution: Find Common Characters
Fizz Buzz (easy)
Solution: Fizz Buzz
Increasing Order Search Tree (easy)
Solution: Increasing Order Search Tree
Largest Odd Number in String (easy)
Solution: Largest Odd Number in String
Best Time to Buy and Sell (easy)
Solution: Best Time to Buy and Sell Stock
Take Gifts From the Richest Pile(easy)
Solution: Take Gifts From the Richest Pile
Remove Duplicates from Sorted List (easy)
Solution: Remove Duplicates from Sorted List
Amazon Medium Problems
Maximum Length of Subarray With Positive Product (medium)
Solution: Maximum Length of Subarray With Positive Product
Largest Submatrix With Rearrangements (medium)
Solution: Largest Submatrix With Rearrangements
Count the Number of Good Subarrays (medium)
Solution: Count the Number of Good Subarrays
Minimum Suffix Flips (medium)
Solution: Minimum Suffix Flips
Minimum Number of Operations to Make Array Empty (medium)
Solution: Minimum Number of Operations to Make Array Empty
Next Greater Element II (medium)
Solution: Next Greater Element II
Make Sum Divisible by P (medium)
Solution: Make Sum Divisible by P
Rotting Oranges (medium)
Solution: Rotting Oranges
Minimum Deletions to Make Character Frequencies Unique (medium)
Solution: Minimum Deletions to Make Character Frequencies Unique
Minimum Adjacent Swaps to Make a Valid Array (medium)
Solution: Minimum Adjacent Swaps to Make a Valid Array
Reverse Integer (medium)
Solution: Reverse Integer
Maximize Distance to Closest Person (medium)
Solution: Maximize Distance to Closest Person
Split Linked List in Parts (medium)
Solution: Split Linked List in Parts
Maximum Difference Between Node and Ancestor (medium)
Solution: Maximum Difference Between Node and Ancestor
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
The kth Factor of n (medium)
Solution: The kth Factor of n
Coin Change II (medium)
Solution: Coin Change II
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Valid Sudoku (medium)
Solution: Valid Sudoku
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Next Permutation (medium)
Solution: Next Permutation
Subsets (medium)
Solution: Subsets
Combination Sum (medium)
Solution: Combination Sum
Number of Provinces (medium)
Solution: Number of Provinces
Number of Islands (medium)
Solution: Number of Islands
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Solution: Minimum Deletions to Make Character Frequencies Unique

Problem Statement

Given a string str, return the minimum number of characters you need to delete to make str good.

A good string doesn't contain two different characters with the same frequency.

Examples

Example 1:

  • Input: "aabbbb"
  • Expected Output: 0
  • Justification: In the given string, the frequency of a is 2, and the frequency of b is 4. Each character already has a unique frequency. So, no need to delete any character from the string

Example 2:

  • Input: "aaaabbbbcc"
  • Expected Output: 1
  • Justification: The character 'c' appears twice, and 'a' and 'b' appear four times each. To make all frequencies unique, we can delete one occurrence of either 'a' or 'b'. This leaves us with 'a', 'b', and 'c' having frequencies of 3, 4, and 2, respectively.

Example 3:

  • Input: "aabbcc"
  • Expected Output: 3
  • Justification: Here, each character's frequncey is 2. By deleting both a and 1 b, we can make frequency unique.

Solution

To solve this problem, we can adopt a strategy that focuses on counting the frequency of each character in the string and then adjusting those frequencies to ensure uniqueness. The initial step involves creating a frequency count for each character. Once we have this frequency map, we'll sort these frequencies in descending order. This allows us to iteratively adjust the higher frequencies to make them unique, minimizing the total number of deletions required. This approach works because by starting from the highest frequencies, we ensure that any necessary adjustments result in the minimal possible change, thereby preserving as much of the original string as possible.

This method is effective because it directly addresses the requirement for uniqueness in character frequencies by systematically reducing frequencies from the highest down, ensuring that each adjustment contributes to the goal with the minimal number of deletions. By doing so, it avoids unnecessary deletions and efficiently achieves the target state with optimal moves.

Step-by-step algorithm

  1. Count Frequencies: First, count the frequency of each character in the string.
  2. Sort Frequencies: Sort these frequencies in descending order.
  3. Adjust Frequencies: Initialize a variable to keep track of the total deletions. For each frequency starting from the second highest, do the following:
    • If the current frequency is equal to or greater than the previous frequency, decrease it to one less than the previous frequency (if the previous frequency is greater than 0).
    • Increase the total deletions count by the number of deletions made to adjust the current frequency.
    • Update the previous frequency to the current (adjusted) frequency.
  4. Return Total Deletions: The total deletions variable now contains the minimum number of deletions required to ensure all character frequencies are unique.

Algorithm Walkthrough

Let's consider the Input: "aaaabbbbcc"

  1. Count Frequencies:

    • Count the frequency of each character in the string.
    • Frequencies: {'a': 4, 'b': 4, 'c': 2}
  2. Sort Frequencies:

    • Sort the frequencies in descending order to address the highest frequencies first, which gives us [4, 4, 2].
  3. Adjust Frequencies:

    • Start with the second highest frequency (since the first one does not have a preceding frequency to compare with) and make adjustments to ensure each frequency is unique.

    First Iteration (Second '4'):

    • The first '4' (for 'a') has no conflict and remains unchanged.
    • The second '4' (for 'b') is equal to the first '4'. To make it unique, we need to reduce it to the frequency[i - 1] - 1 = frequency[0] - 1 = 4 - 1 = 3. .
    • Action: Reduce the second '4' to '3'.
    • Deletions: 1 (from 'b').
    • Frequencies now: [4 (a's frequency, unchanged), 3 (adjusted b's frequency), 2 (c's unchanged frequency)].

    No Further Adjustments Needed:

    • The next frequency is '2' (for 'c'), which is already unique in the context of the adjusted frequencies.
    • No adjustment or deletion is required for 'c'.
  4. Total Deletions:

    • The total deletions required to make the frequencies unique are 1 (one deletion from 'b').

Code

Java
Saved to Cloud

. . . .

Complexity Analysis:

Time Complexity

  • The time complexity is , where (n) is the length of the string and (k) is the number of unique characters in the string.
  • Counting frequencies takes time.
  • Sorting the frequencies takes time, where (k) is the number of unique characters.
  • The loop to adjust frequencies and calculate deletions takes time.

Space Complexity

The space complexity is , where (k) is the number of unique characters in the string. This is due to the additional storage needed for the frequency map and the list of frequencies.

Minimum Deletions to Make Character Frequencies Unique (medium)
Minimum Adjacent Swaps to Make a Valid Array (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step algorithm

Algorithm Walkthrough

Code

Complexity Analysis:

Time Complexity

Space Complexity